package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.OrganizationService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.OrganizationBean;

public class OrganizationAction extends BaseAction {

	private static final long serialVersionUID = 4238148441703158787L;
	private OrganizationService organizationService;
	private List<OrganizationBean> resultList;
	private OrganizationBean orgBean;
	private String orgId;
	private FileBean fileBean;

	public List<OrganizationBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<OrganizationBean> resultList) {
		this.resultList = resultList;
	}

	public OrganizationBean getOrgBean() {
		return orgBean;
	}

	public void setOrgBean(OrganizationBean orgBean) {
		this.orgBean = orgBean;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public FileBean getFileBean() {
		return fileBean;
	}

	public void setFileBean(FileBean fileBean) {
		this.fileBean = fileBean;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public String list(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			resultList = organizationService.queryOrgs(streetId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}
	

	public String initUpdate(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			orgBean = organizationService.queryOrg(streetId, orgId);
			super.getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String update(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(orgBean != null){
			orgBean.setStreetId(streetId);
			orgBean.setFileBean(fileBean);
			orgBean.setPhotoPath(super.getText("custom.org.img.path"));
			orgBean.setPhotoDir(super.getText("custom.org.img.dir"));
		}
		try {
			organizationService.updateOrg(orgBean);
		} catch (BaseException e) {
			super.getRequest().setAttribute("method", "update");
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String add(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(orgBean != null){
			orgBean.setStreetId(streetId);
			orgBean.setFileBean(fileBean);
			orgBean.setPhotoPath(super.getText("custom.org.img.path"));
			orgBean.setPhotoDir(super.getText("custom.org.img.dir"));
		}
		try {
			organizationService.addOrg(orgBean);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String del(){
		try {
			organizationService.delOrg(orgId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
}
